{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dressmaker - Easy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ········\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "import psycopg2\n",
    "from sqlalchemy import create_engine\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "pwd = getpass.getpass()\n",
    "engine = create_engine(\n",
    "    'postgresql+psycopg2://postgres:%s@192.168.31.31:15432/sqlzoo' % (pwd))\n",
    "pd.set_option('display.max_rows', 60)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "jmcust = pd.read_sql_table('jmcust', engine)\n",
    "dressmaker = pd.read_sql_table('dressmaker', engine)\n",
    "dress_order = pd.read_sql_table('dress_order', engine)\n",
    "construction = pd.read_sql_table('construction', engine)\n",
    "quantities = pd.read_sql_table('quantities', engine)\n",
    "order_line = pd.read_sql_table('order_line', engine)\n",
    "garment = pd.read_sql_table('garment', engine)\n",
    "material = pd.read_sql_table('material', engine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "List the post code, order number, order date and garment descriptions for all items associated with Ms Brown."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>post_code</th>\n",
       "      <th>order_no</th>\n",
       "      <th>order_date</th>\n",
       "      <th>description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>B2 5AB</td>\n",
       "      <td>7</td>\n",
       "      <td>2002-02-21</td>\n",
       "      <td>Short Skirt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B2 5AB</td>\n",
       "      <td>7</td>\n",
       "      <td>2002-02-21</td>\n",
       "      <td>Sundress</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>B2 5AB</td>\n",
       "      <td>7</td>\n",
       "      <td>2002-02-21</td>\n",
       "      <td>Suntop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B2 5AB</td>\n",
       "      <td>9</td>\n",
       "      <td>2002-02-27</td>\n",
       "      <td>Shorts</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   post_code  order_no order_date           description\n",
       "0  B2 5AB            7 2002-02-21  Short Skirt         \n",
       "1  B2 5AB            7 2002-02-21  Sundress            \n",
       "2  B2 5AB            7 2002-02-21  Suntop              \n",
       "3  B2 5AB            9 2002-02-27  Shorts              "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = (jmcust.loc[jmcust['c_name'].str.strip()=='Ms Brown']\n",
    "      .merge(dress_order, left_on='c_no', right_on='cust_no')\n",
    "      .merge(order_line, left_on='order_no', right_on='order_ref')\n",
    "      .merge(garment, left_on='ol_style', right_on='style_no')\n",
    "      .rename(columns={'c_post_code': 'post_code'})\n",
    "      [['post_code', 'order_no', 'order_date', 'description']])\n",
    "t2 = (dressmaker.loc[dressmaker['d_name'].str.strip()=='Ms Brown']\n",
    "      .merge(construction, left_on='d_no', right_on='maker')\n",
    "      .merge(dress_order, left_on='order_ref', right_on='order_no')\n",
    "      .merge(order_line, left_on='order_no', right_on='order_ref')\n",
    "      .merge(garment, left_on='ol_style', right_on='style_no')\n",
    "      .rename(columns={'d_post_code': 'post_code'})\n",
    "      [['post_code', 'order_no', 'order_date', 'description']])\n",
    "pd.concat([t1, t2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "List the customer name, postal information, order date and order number of all orders that have been completed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>c_name</th>\n",
       "      <th>c_post_code</th>\n",
       "      <th>order_date</th>\n",
       "      <th>order_no</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ms White</td>\n",
       "      <td>E24 8PQ</td>\n",
       "      <td>2002-02-03</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ms White</td>\n",
       "      <td>E24 8PQ</td>\n",
       "      <td>2002-02-03</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ms White</td>\n",
       "      <td>E24 8PQ</td>\n",
       "      <td>2002-02-03</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Mr Brass</td>\n",
       "      <td>FG24 9NM</td>\n",
       "      <td>2002-02-02</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Mr Brass</td>\n",
       "      <td>FG24 9NM</td>\n",
       "      <td>2002-02-02</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Ms Muir</td>\n",
       "      <td>H2 7CV</td>\n",
       "      <td>2002-01-20</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Dr Green</td>\n",
       "      <td>SJ4 4WE</td>\n",
       "      <td>2002-01-11</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Dr Green</td>\n",
       "      <td>SJ4 4WE</td>\n",
       "      <td>2002-01-11</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Dr Green</td>\n",
       "      <td>SJ4 4WE</td>\n",
       "      <td>2002-01-11</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Mrs Peacock</td>\n",
       "      <td>DD6 9NM</td>\n",
       "      <td>2002-01-10</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Mrs Peacock</td>\n",
       "      <td>DD6 9NM</td>\n",
       "      <td>2002-01-10</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  c_name c_post_code order_date  order_no\n",
       "0   Ms White               E24 8PQ   2002-02-03         5\n",
       "1   Ms White               E24 8PQ   2002-02-03         5\n",
       "2   Ms White               E24 8PQ   2002-02-03         5\n",
       "3   Mr Brass               FG24 9NM  2002-02-02         4\n",
       "4   Mr Brass               FG24 9NM  2002-02-02         4\n",
       "5   Ms Muir                H2 7CV    2002-01-20         3\n",
       "6   Dr Green               SJ4 4WE   2002-01-11         2\n",
       "7   Dr Green               SJ4 4WE   2002-01-11         2\n",
       "8   Dr Green               SJ4 4WE   2002-01-11         2\n",
       "9   Mrs Peacock            DD6 9NM   2002-01-10         1\n",
       "10  Mrs Peacock            DD6 9NM   2002-01-10         1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(jmcust.merge(dress_order.loc[dress_order['completed']=='Y'],\n",
    "              left_on='c_no', right_on='cust_no')\n",
    " .merge(order_line, left_on='order_no', right_on='order_ref')\n",
    " [['c_name', 'c_post_code', 'order_date', 'order_no']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "Which garments have been made or are being made from 'red abstract' or 'blue abstract' coloured materials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>style_no</th>\n",
       "      <th>description</th>\n",
       "      <th>colour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Trousers</td>\n",
       "      <td>Red Abstract</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Long Skirt</td>\n",
       "      <td>Red Abstract</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>Sundress</td>\n",
       "      <td>Red Abstract</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Long Skirt</td>\n",
       "      <td>Blue Abstract</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Short Skirt</td>\n",
       "      <td>Blue Abstract</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   style_no           description                colour\n",
       "0         1  Trousers              Red Abstract        \n",
       "1         2  Long Skirt            Red Abstract        \n",
       "2         5  Sundress              Red Abstract        \n",
       "3         2  Long Skirt            Blue Abstract       \n",
       "4         4  Short Skirt           Blue Abstract       "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(garment.merge(order_line, left_on='style_no', right_on='ol_style')\n",
    " .merge(material.loc[material['colour'].str.strip()\n",
    "        .str.lower().isin(['red abstract', 'blue abstract'])],\n",
    "        left_on='ol_material', right_on='material_no')\n",
    " [['style_no', 'description', 'colour']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "How many garments has each dressmaker constructed? You should give the number of garments and the name and postal information of each dressmaker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>d_no</th>\n",
       "      <th>d_name</th>\n",
       "      <th>d_post_code</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Mrs Hem</td>\n",
       "      <td>A12 6BC</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Miss Stitch</td>\n",
       "      <td>DF4 7HJ</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Mr Needles</td>\n",
       "      <td>E12 6LG</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Ms Sew</td>\n",
       "      <td>EF7 9KL</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Mr Seam</td>\n",
       "      <td>H45 7YH</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Mr Taylor</td>\n",
       "      <td>SH6 9RT</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Miss Pins</td>\n",
       "      <td>B4 9BL</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   d_no                d_name d_post_code  cnt\n",
       "0     1  Mrs Hem                 A12 6BC    14\n",
       "1     2  Miss Stitch             DF4 7HJ    20\n",
       "2     3  Mr Needles              E12 6LG    12\n",
       "3     4  Ms Sew                  EF7 9KL    12\n",
       "4     5  Mr Seam                 H45 7YH    12\n",
       "5     6  Mr Taylor               SH6 9RT     7\n",
       "6     7  Miss Pins               B4 9BL     11"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(dressmaker.merge(construction, left_on='d_no', right_on='maker')\n",
    " .merge(dress_order, left_on='order_ref', right_on='order_no')\n",
    " .merge(order_line, left_on='order_no', right_on='order_ref')\n",
    " .merge(garment, left_on='ol_style', right_on='style_no')\n",
    " .groupby(['d_no', 'd_name', 'd_post_code'])\n",
    " .agg(cnt=pd.NamedAgg(column='line_no', aggfunc='count'))\n",
    " .reset_index())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "Give the names of those dressmakers who have finished items made from silk for completed orders."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>d_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Mrs Hem</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Miss Stitch</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Miss Pins</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Mr Needles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Ms Sew</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 d_name\n",
       "0  Mrs Hem             \n",
       "1  Miss Stitch         \n",
       "5  Miss Pins           \n",
       "7  Mr Needles          \n",
       "8  Ms Sew              "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(dressmaker.merge(construction, left_on='d_no', right_on='maker')\n",
    " .merge(dress_order.loc[dress_order['completed']=='Y'], \n",
    "        left_on='order_ref', right_on='order_no')\n",
    " .merge(order_line, left_on='order_no', right_on='order_ref')\n",
    " .merge(garment, left_on='ol_style', right_on='style_no')\n",
    " .merge(material.loc[material['fabric'].str.strip().str.lower()=='silk'], \n",
    "        left_on='ol_material', right_on='material_no')\n",
    " [['d_name']].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
